package mh.map;

import java.awt.Rectangle;
import java.util.Properties;

import mh.interfaces.Constants;

/**
 * A Layer of the Map, this class is abstract because must be extended for each type of level
 * 
 * @author Gianmarco Laggia
 * @since 16/08/2013
 */
public abstract class Layer implements Constants {

	/**
	 * The visibility of the layer, true if visible.
	 */
	protected boolean	visible;
	/**
	 * The name of this layer
	 */
	protected String	name;
	/**
	 * Some properties of this layer
	 */
	private Properties	properties		= new Properties();
	/**
	 * The opacity of this layer, from 0 to 1. Default 1
	 */
	protected float		opacity;
	/**
	 * The X position of the player in the map (tile unit)
	 */
	protected double	actualx;
	/**
	 * The Y position of the player in the map (tile unit)
	 */
	protected double	actualy;
	/**
	 * The X position of the player the map has to reach (tile unit)
	 */
	protected double	finalx;
	/**
	 * The Y position of the player the map has to reach (tile unit)
	 */
	protected double	finaly;
	protected double	lastFrameChange;
	protected double	frameDuration	= VELOCITY / 2;	// maybe type long is better
	protected Map		map;
	protected Rectangle	bounds;

	/**
	 * 
	 * @param x
	 *            The initial x position of the player when created the map
	 * @param y
	 *            The initial y position of the player when created the map
	 * @param width
	 *            The width of the layer
	 * @param height
	 *            The height of the layer
	 */
	public Layer(double x, double y, int width, int height) {
		this.actualx = x;
		this.actualy = y;
		this.finalx = x;
		this.finaly = y;
		this.bounds = new Rectangle(width, height);
	}

	/**
	 * The method called each time the window is refreshed (about 60fps).
	 * 
	 * @param delta
	 *            time passed since the previous call of the method.
	 */
	public abstract void draw(long delta);

	/**
	 * The x position of the player in the layer
	 * 
	 * @return double
	 */
	public double getPosX() {
		return this.actualx;
	}

	/**
	 * The y position of the player in the layer
	 * 
	 * @return double
	 */
	public double getPosY() {
		return this.actualy;
	}

	/**
	 * Used to change the position where the layer is drawed.
	 * 
	 * @param dx
	 * @param dy
	 */
	public void setBounds(int dx, int dy) {
		this.finalx += dx;
		this.finaly += dy;
	}

	/**
	 * Used to check if a tile is free to make a move.
	 * 
	 * @param x
	 *            The x coord to check
	 * @param y
	 *            The y coord to check
	 * @return true if avaible
	 */
	public abstract boolean isAvaible(int x, int y);

	/**
	 * Sets the layer to visible (true) or not (false)
	 * 
	 * @param vis
	 *            True if visible
	 */
	public void setVisible(boolean vis) {
		this.visible = vis;
	}

	/**
	 * Sets layer opacity.
	 * 
	 * @param opacity
	 *            the new opacity for this layer
	 */
	public void setOpacity(float opacity) {
		this.opacity = opacity;
	}

	/**
	 * Sets the name of this layer
	 * 
	 * @param n
	 */
	public void setName(String n) {
		this.name = n;
	}

	/**
	 * Returns the name of this layer
	 * 
	 * @return the name
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Gets the properties of the layer
	 * 
	 * @return a <code>Properties</code> set
	 */
	public Properties getProperties() {
		return this.properties;
	}

	/**
	 * Set the map in witch this layer is in.
	 * 
	 * @param m
	 */
	public void setMap(Map m) {
		this.map = m;
	}

}
